// by Xeno
//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_repair\fn_repair.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_vehicle","_caller","_coef","_damage","_damage_ok","_damage_val","_fuel","_fuel_ok","_fuel_val","_rep_count","_breaked_out","_rep_action","_type_name", "_exitit"
		,"_items","_current","_repair"];

DEFAULT_PARAM(_caller,0,objNull);
DEFAULT_PARAM(_vehicle,1,objNull);

GVAR(Repairing) = true;

_rep_count = switch (true) do {
	case (_vehicle isKindOf "Air"): {GVAR(AirRepairDivider)};
	case (_vehicle isKindOf "Tank" || _vehicle isKindOf "Tracked_APC"): {GVAR(TankRepairDivider)};
	default {GVAR(VehicleRepairDivider)};
};
if ((_caller distance _vehicle) > 7) exitWith { GVAR(Repairing) = false; };

_damage = damage _vehicle;

_damage_val = _damage / _rep_count;
if (GVAR(is_engineer)) then {
	_damage_val = ceil(_damage_val / 3);
};
_coef = (ceil _damage_val) max 1;
_estimated_time = _damage_val * 3;
_rep_count = (_damage / _coef);

_exitit = false;

if !(local _caller) exitWith {};

_type_name = ([typeOf (_vehicle),0] call mso_fnc_GetDisplayName);

TRACE_1("Start Repair",ARR_4(_type_name,_damage,_coef,_estimated_time));

GVAR(cancelrep) = false;
_damage_ok = false;
_breaked_out = false;
_breaked_out2 = false;
systemChat (format[localize "STR_TYR_REPAIR_VEHICLE",_type_name]);
_rep_action = _caller addAction[RED_TEXT(localize "STR_TYR_REPAIR_ACTION_CANCEL"),{GVAR(cancelrep) = true},[],100,false,true];
TRACE_1("Cancel Action",_rep_action);
for "_wc" from 1 to _coef do {
	if (GVAR(cancelrep)) exitWith {
		systemChat (localize "STR_TYR_REPAIR_CANCELED");
		_breaked_out = true;
	};
	if (!alive _caller || !alive _vehicle || vehicle _caller != _caller || ((_caller distance _vehicle) > 7)) exitWith {
		_breaked_out2 = true;
	};
	systemChat (localize "STR_TYR_REPAIR_REPAIRING");
	_caller playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 3.0;
	waitUntil {animationState _caller != "AinvPknlMstpSlayWrflDnon_medic"};
	TRACE_1("Repairing",ARR_3(GVAR(cancelrep),_damage,_rep_count));
	if (GVAR(cancelrep) && (_damage > 0.01)) exitWith {
		systemChat (localize "STR_TYR_REPAIR_CANCELED");
		_breaked_out = true;
	};
	if (!alive _caller || !alive _vehicle || vehicle _caller != _caller || ((_caller distance _vehicle) > 7)) exitWith {
		_breaked_out2 = true;
	};
	if (!_damage_ok) then {_damage = _damage - _rep_count};
	if (_damage <= 0.01 && {!_damage_ok}) then {_damage_ok = true;};
};
TRACE_1("Stopped Repair",ARR_3(_damage,_breaked_out,_breaked_out2));
_caller removeAction _rep_action;
if (alive _vehicle) then {
	[0,[_vehicle,_damage],QUOTE(FUNC(applyRepair))] call mso_core_fnc_ExMP;
};

// We remove the toolkit for non engineers
TRACE_1("Removing Toolkit",ARR_3(GVAR(is_engineer),(items _caller),(itemCargo _vehicle)));
if (!GVAR(is_engineer) && {(alive _caller && {("ToolKit" in (items _caller))}) || ({"ToolKit" == _x} count (itemCargo _vehicle)) > 0}) then {
	if (({"ToolKit" == _x} count (itemCargo _vehicle)) > 0) then {
		_items = itemCargo _vehicle;
		clearItemCargoGlobal _vehicle;
		{
			_current = _x;
			if (_current == "ToolKit") exitWith {
				_items set [_foreachIndex, "XxDelxX"];
			};
		} foreach _items;
		_items = _items - ["XxDelxX"];
		TRACE_1("Updated vehicle items",_items);
		{
			_vehicle addItemCargoGlobal [_x,1];;
		} foreach _items;
	} else {
		_caller removeItem "ToolKit";
	};
};
systemChat format [(localize "STR_TYR_REPAIR_DONE"), _type_name];

GVAR(Repairing) = false;
TRACE("End");
TMFPROFILERSTOP;
